<?php
class IndexAction extends Action {
    public function index(){

		$dataModel = D('Data');

		//初始化模型
		$PhotographicModel = D('Photographic');
		$images = $PhotographicModel->getLastestPics();

		if(empty($images)){
			$images = array_fill(0, 4, array());
		}
		$this->images = $images;


		$time = time();
		$range = array(
			'10分钟雨量'	=>	$time - 600,
			'1小时雨量'		=>	$time - 3600,
			'6小时雨量'		=>	$time - 3600 * 6,
			'24小时雨量'	=>	$time - 3600 * 24,
			'48小时雨量'	=>	$time - 3600 * 48,
			'72小时雨量'	=>	$time - 3600 * 72,
		);
		$totalByRange = $dataModel->getTotalByRange($range, DataModel::TYPE_RAIN);
		foreach ($totalByRange as &$val) {
			$val['val1'] = sprintf('%.2f', $val['val1']);
			$val['val2'] = sprintf('%.2f', $val['val2']);
		}
		$this->totalByRange = $totalByRange;

		$this->display();
    }
	/**
	*	首页风速1图表
	*
	*	@param
	*	@return
	*	@date
	*	@author lin.x
	*
	*/
	public function getchart(){

		$dataModel = D('Data');

		import('@.Widget.Chart');
		$chart = new Chart;

		$id = $_GET['id'];
		switch($id){
			case 'chart1':
			//设备1
			$dataModel = $dataModel->setInterval(300)
				->setName('设备1')
				->setTimeRange(time() - 3600)
				->setDataType(DataModel::TYPE_WIND)
				->setDevice(1);

			$html = $chart->setTitle('风速监测')
				->setSubTitle(date('Y-m-d'))
				->setLabel('风速 单位m/s')
				->setDataSource($dataModel)
				->getChart();
			break;
			case 'chart2':
			//设备2
			$dataModel = $dataModel->setInterval(300)
				->setName('设备2')
				->setTimeRange(time() - 3600)
				->setDataType(DataModel::TYPE_RAIN)
				->setDevice(2);

			$html = $chart->setTitle('雨量监测')
				->setSubTitle(date('Y-m-d'))
				->setLabel('雨量 单位mm')
				->setDataSource($dataModel)
				->getChart();
			break;
		}
		if($this->isAjax()){
			echo $html;
		}else{
			$this->html = $html;
			$this->display();
		}
	}

	public function login(){
		if($_POST){
			if($_POST['password'] == C('password')){
				setcookie('password', md5($_REQUEST['password']), time() + 3600 * 24 * 365, '/');
				redirect($_REQUEST['redirect']);
			}else{
				$this->msg = '密码不正确!';
			}
		}
		$this->display();
	}

	public function logout(){
		setcookie('password', null, time(), '/');
		redirect(U('/'));
	}

	/**
	*	获取连续播放图片
	*
	*	@param
	*	@return
	*	@date
	*	@author lin.x
	*
	*/
	public function getimages(){
		import('@.Widget.ImageRotate');
		$ImageRotate = new ImageRotate;
		$PhotographicModel = D('Photographic');

		$size = C('IMAGE_THUMB_SIZE');
		list($key, $val) = each($size);
		$id = addslashes($_GET['id']);
		$id = $id ? $id : $key;

		$imgs = $PhotographicModel->getList($id, 0);


		//获取 600*450 图片
		$imgs = $PhotographicModel->getSize($imgs, $size[1]);

		foreach ($imgs as $key=>$val) {
			$rows[] = array('src'=>$val, 'dateline'=>date('Y-m-d H:i:s', $key));
		}

		//第一组轮播图片
		$html = $ImageRotate->getHtml($rows);
		echo json_encode(array(
			'width'		=>	$size[1][0],
			'height'	=>	$size[1][1],
			'content'	=>	$html
		));

	}
	/**
	*	更新图片
	*
	*	@param
	*	@return
	*	@date	2012-9-27 21:02:20
	*	@author lin.x
	*
	*/
	public function update(){
		//初始化模型
		$PhotographicModel = D('Photographic');
		$images = $PhotographicModel->getLastestPics();

		$dataModel = D('Data');

		//获取最新的风速 字段对应为val1, val2
		$res = $dataModel->getLastestData('val1, val2');

		exit(json_encode(array(
			'data'	=> array(
				'images'	=>	$images,
				'rain'		=>	array(
					$res['val1'] * $dataModel->data_convert_coefficient[DataModel::TYPE_WIND],
					$res['val2'] * $dataModel->data_convert_coefficient[DataModel::TYPE_WIND],
				),
			),
		)));
	}

}